package com.andy.blog.admin.controller;

import com.andy.blog.admin.dto.SensitiveDTO;
import com.andy.blog.admin.service.SensitiveBusService;
import com.andy.blog.common.annotation.Log;
import com.andy.blog.common.enums.BusinessTypeEnum;
import com.andy.blog.common.page.PageUtilsDTO;
import com.andy.blog.common.reponse.GeneralResponseResult;
import com.andy.blog.dal.vo.SensitiveVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by andy on 2023/7/21 22:33
 */
@RestController
@RequestMapping("/system/sensitive")
public class SensitiveController {

    @Resource
    private SensitiveBusService sensitiveBusService;

    /**
     * 获取参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:sensitive:list')")
    @GetMapping("/list")
    public GeneralResponseResult<PageUtilsDTO<SensitiveDTO>> list(SensitiveVO sensitiveVO) {
        return GeneralResponseResult.success(sensitiveBusService.selectPage(sensitiveVO));
    }

    /**
     * 新增参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:sensitive:add')")
    @Log(title = "敏感词管理", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    public GeneralResponseResult<Void> add(@Validated @RequestBody SensitiveVO sensitiveVO) {
        sensitiveBusService.add(sensitiveVO);
        return GeneralResponseResult.success();
    }

    /**
     * 根据id查询
     */
    @PreAuthorize("@ss.hasPermi('system:sensitive:query')")
    @GetMapping(value = "/{id}")
    public GeneralResponseResult<SensitiveDTO> getDetail(@PathVariable Long id) {
        return GeneralResponseResult.success(sensitiveBusService.selectById(id));
    }

    /**
     * 修改通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:sensitive:edit')")
    @Log(title = "修改敏感词", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    public GeneralResponseResult<Void> edit(@Validated @RequestBody SensitiveVO sensitiveVO) {
        sensitiveBusService.updateById(sensitiveVO);
        return GeneralResponseResult.success();
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('system:sensitive:remove')")
    @Log(title = "删除敏感词", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{id}")
    public GeneralResponseResult<Void> remove(@PathVariable Long id) {
        sensitiveBusService.deleteById(id);
        return GeneralResponseResult.success();
    }

    /**
     * 刷新字典缓存
     */
    @PreAuthorize("@ss.hasPermi('system:sensitive:remove')")
    @Log(title = "刷新敏感词", businessType = BusinessTypeEnum.CLEAN)
    @GetMapping("/refreshSensitive")
    public GeneralResponseResult<Void> refreshSensitive() {
        sensitiveBusService.refreshSensitive();
        return GeneralResponseResult.success();
    }
}
